<?php

namespace App\Admin\Controller\Cms;

class Tag extends \App\Admin\Controller\Authed {

    public function index() {

        $do = $this->request->get('do');

        if ($do == 'sort') {
            $sorts = $this->request->getPost('sorts');

            foreach ($sorts as $key => $value) {
                $result = Cms()->Tag()->setSort($key, $value);

                if (!$result) {
                    fail();
                }
            }

            succ();
        }

        $rows = Cms()->Tag()->getAll();

        foreach ($rows as &$row) {
            $row['state_title'] = Cms()->Tag()->getStateAssoc($row['state']);
        }
        unset($row);

        $this->rows = $rows;
    }

    public function edit() {
        
        $this->enablePageCache(true);

        $id = intval($this->request->get('id'));

        if ($this->request->isPost()) {

            $data = $this->request->get();

            if ($id) {
                $result = Cms()->Tag()->update($id, $data);
                if ($result) {
                    succ('', '', 2);
                } else {
                    fail();
                }
            } else {
                $result = Cms()->Tag()->add($data);
                if ($result) {
                    succ('', '', 2);
                } else {
                    fail();
                }
            }
        }

        $this->detail = Cms()->Tag()->get($id);

        $this->states = Cms()->Tag()->getStateAssoc();
    }

    public function remove() {

        $id = intval($this->request->get('id'));

        $result = Cms()->Tag()->remove($id);

        if ($result) {
            succ();
        } else {
            fail();
        }
    }

    public function article() {

        $do = $this->request->get('do');

        $this->tag_id = $tag_id = intval($this->request->get('tag_id'));

        if ($do == 'remove') {

            $article_id = intval($this->request->get('article_id'));

            $result = Cms()->Article()->Tag()->removeByArticleTag($article_id, $tag_id);

            if ($result) {
                succ();
            } else {
                fail();
            }

            return;
        }

        $count = Cms()->Article()->Tag()->countArticleIdsByTag($tag_id);

        /* 分页 */
        $pager = $this->_createPager($count, ['tag_id' => $tag_id]);
        $length = $pager['length'];
        $start = $pager['offset'];
        $this->assign('pager', $pager);

        $article_ids = Cms()->Article()->Tag()->getArticleIdsByTag($tag_id, $start, $length);

        if ($article_ids) {

            $filter['id'] = ['in' => $article_ids];

            $rows = Cms()->Article()->getAll(null, null, $filter);

            foreach ($rows as &$row) {
                $row['column_title'] = Cms()->Column()->get($row['column_id'], 'title');
                $row['type_title'] = Cms()->Article()->getTypeAssoc($row['type']);
                $row['state_title'] = Cms()->Article()->getStateAssoc($row['state']);
            }

            $this->rows = $rows;
        }
    }

}
